<?php

App::uses('AppModel', 'Model');

class Slideshow extends AppModel {
	public $name = 'Slideshow';
	
	public function truncate() {
		$this->query("TRUNCATE TABLE {$this->table}");
	}
	
	public function beforeSave($options = array()) {
		if(!$this->data['Slideshow']['file']['error']) {
			$file = $this->data['Slideshow']['file'];
			$destination = "img" . DS . "showcase" . DS . $file['name'];
			
			if(!move_uploaded_file($file['tmp_name'], $destination)) {
				return false;
			}else{
				$this->data['Slideshow']['image'] = $file['name'];
			}
		}
		
		$this->data['Slideshow']['link'] = trim($this->data['Slideshow']['link']);
		if(!empty($this->data['Slideshow']['link']) && !preg_match('/^[fh]t+ps?:\/\//', $this->data['Slideshow']['link'])) {
			$this->data['Slideshow']['link'] = 'http://' . $this->data['Slideshow']['link'];
		}
		
		return true;
	}
}
